
function Validate (url) 
{
	var self = this;
	
	this.id = null;
	this.url = url;
	
	this.init = function(id) {
		self.id = id;
		console.log(self.id);
		$('#' + self.id +' input').blur(function(){
			var id = $(this).parent().prev().find('label').attr('for');
			setTimeout(function(){self.doValidation(id);}, 500);
		});
	}
	
	this.doValidation = function(id) {
		var data = {};

		$('#' + self.id + ' input').each(function(){
			if( $(this).get(0).tagName == 'select') {
				id = $(this).attr('id');
				data[$(this).attr('name')] = $('#'+id+' :selected').val();
			} else {
				data[$(this).attr('name')] = $(this).val();
			}
		});

		$('#'+id).parent().find('.errors').remove();
		
		$.post(self.url, data, function(response){
			if( response[id] ) {
				$('#'+id).parent().append(this.getErrorHtml(response[id], id));
			}
		}, 'json');
	}
	
	this.getErrorHtml = function (formErrors, id)
	{
		var o = '<ul id="errors-'+id+'" class="errors">';
		for( errorKey in formErrors) {
			o += '<li>' + formErrors[errorKey] + '</li>';
		}
		o += '</ul>';
		return o;
	}
}
